// miniprogram/pages/index/index.js
let app = getApp();
let utils = require('../../utils/util.js')
let network = require('../../utils/requestManager.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_show:true,
    currentTab: 0,
    morning_data:{
      title:"修改组件的数据传递"
    },
    signUp_data:{
      title: "微报名修改组件的数据传递"
    },
    recruit_data: {
      title: "招聘修改组件的数据传递"
    },
    show_personal_data:{//展示端按钮
      share:true,
    },
    edit_personal_data:{//编辑端按钮
      edit_morning: true,
      share_morning: true,
      new_wei_activity: false,
      new_job: false,
      edit_personal_center: true
    },
    tabArray: [{
      name:"早安心语",
      imageActive:"https://extend.qnssl.xuemei99.com/icon_morning_active.png",
      imageDefault: "https://extend.qnssl.xuemei99.com/icon_morning_default.png",
    }, {
        name: "微报名",
        imageActive: "https://extend.qnssl.xuemei99.com/icon_signup_active.png",
        imageDefault: "https://extend.qnssl.xuemei99.com/icon_signup_default.png",
      }, {
        name: "招聘",
        imageActive: "https://extend.qnssl.xuemei99.com/icon_recruit_active.png",
        imageDefault: "https://extend.qnssl.xuemei99.com/icon_recruit_default.png",
      }]
  },

  /**
   * 点击tab切换
   * 编辑端
   * ===0:显示编辑和分享按钮
   * ===1：显示新建活动按钮
   * ===2：显示新建职位按钮
   * **/
  swichNav:function(e){
    let that = this;
    let now_current = e.target.dataset.current;
    //展示端
    let show_share = "show_personal_data.share"
    
    //编辑端
    let edit_morning = "edit_personal_data.edit_morning"
    let share_morning = "edit_personal_data.share_morning"
    let new_wei_activity = "edit_personal_data.new_wei_activity"
    let new_job = "edit_personal_data.new_job"
    if (now_current === 0){
      that.setData({
        //展示端
        [show_share]:true,
        //编辑端
        [edit_morning]: true,
        [share_morning]: true,
        [new_wei_activity]: false,
        [new_job]: false,
      })
    }else if(now_current === 1){
      that.setData({
        [show_share]: false,
        [edit_morning]: false,
        [share_morning]: false,
        [new_wei_activity]: true,
        [new_job]: false,
      })
    }else{
      that.setData({
        [show_share]: false,
        [edit_morning]: false,
        [share_morning]: false,
        [new_wei_activity]: false,
        [new_job]: true,
      })
    }
    // return
    if (this.data.currentTab === e.target.dataset.current){
      return false;
    }else{
      that.setData({
        currentTab:e.target.dataset.current
      })
    }
  },

  /**
  * 左右滑动切换swiper
  * **/
  bindChange:function(e){
    let that = this;
    let now_current = e.detail.current;
    //展示端
    let show_share = "show_personal_data.share"

    //编辑端
    let edit_morning = "edit_personal_data.edit_morning"
    let share_morning = "edit_personal_data.share_morning"
    let new_wei_activity = "edit_personal_data.new_wei_activity"
    let new_job = "edit_personal_data.new_job"
    if (now_current === 0) {
      that.setData({
        [show_share]: true,
        [edit_morning]: true,
        [share_morning]: true,
        [new_wei_activity]: false,
        [new_job]: false,
      })
    } else if (now_current === 1) {
      that.setData({
        [show_share]: false,
        [edit_morning]: false,
        [share_morning]: false,
        [new_wei_activity]: true,
        [new_job]: false,
      })
    } else {
      that.setData({
        [show_share]: false,
        [edit_morning]: false,
        [share_morning]: false,
        [new_wei_activity]: false,
        [new_job]: true,
      })
    }
    that.setData({
      currentTab: e.detail.current
    })
  },

  /**
   * 测试
   * 个人中心按钮组件的测试
   * **/
  pageEvent:function(e){
    
  },

  /**
   * 早安心语绑定组件的事件
   * 可获得组件传输过来的数据
   * **/
  morningPage:function(e){
    
  },

  /**
   * 微报名绑定组件事件
   * 可获得组件传输过来的数据
   * **/
  signPage:function(e){
    
  },

  /**
     * 招聘绑定组件事件
     * 可获得组件传输过来的数据
     * **/
  recruitPage:function(e){
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network.login({
      success:function(res){
        console.log(getApp().globalData.owner)
        //判断是否为商家，显示编辑按钮
        if (getApp().globalData.owner) {//true为商家
          that.setData({
            is_show: false,
          })
        }
      },
      fail:function(err){
        err = JSON.stringify(err)
        wx.showModal({
          title: '提示',
          content: '网络出错' + err,
        })
      }
    })
    
    wx.hideShareMenu()//隐藏转发按钮
    console.log(options)
    console.log("打印options")
    if(options.shop_id){//转发进入的更新shop_id
      getApp().globalData.shop_id = options.shop_id
    }
    let that = this;

    
    //初始化数据
    that.setData({
      currentTab: options.currentTab ? options.currentTab : 1//接收制作活动后传递的currentTab
    })
  
    //视窗高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获取组件
    app.personalCenter = this.selectComponent("#personalCenter")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})